package cn.shyouchi.wuguanjia.android.controls;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.utils.AllCapTransformationMethod;
import cn.shyouchi.wuguanjia.android.utils.CheckForAllUtils;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;


public class BasicEditDialogFragment extends DialogFragment {

    @BindView(R.id.basic_edit_dialog_title)
    TextView title;
    @BindView(R.id.basic_edit_dialog_edit_text)
    EditText editText;

    private static final String TAG = "BasicEditDialog";

    private static final String ARG_TITLE = "title";
    private static final String ARG_INPUT_TYPE = "input_type";
    private static final String ARG_CONTENT_HINT = "content_hint";
    private static final String ARG_MULTI_LINE = "multi_line";

    private String mTitle;
    private String mContentHint;
    private int mInputType;
    private boolean mIsMultiLine = false;

    public interface BasicEditDialogListener {
        void onBasicEditDialogPositiveClick(DialogFragment dialog, String content);
    }

    BasicEditDialogListener mListener;


    public void setEnterClickListener(BasicEditDialogListener mListener) {
        this.mListener = mListener;
    }

    public static BasicEditDialogFragment newInstance(String title, String contentHint, int inputType, boolean multiLine) {
        BasicEditDialogFragment fragment = new BasicEditDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_CONTENT_HINT, contentHint);
        args.putInt(ARG_INPUT_TYPE, inputType);
        args.putBoolean(ARG_MULTI_LINE, multiLine);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTitle = getArguments().getString(ARG_TITLE);
            mContentHint = getArguments().getString(ARG_CONTENT_HINT);
            mInputType = getArguments().getInt(ARG_INPUT_TYPE);
            mIsMultiLine = getArguments().getBoolean(ARG_MULTI_LINE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
//        try {
//            // Instantiate the NoticeDialogListener so we can send events to the host
//            mListener = (BasicEditDialogListener) activity;
//        } catch (ClassCastException e) {
//            // The activity doesn't implement the interface, throw exception
//            throw new ClassCastException(activity.toString()
//                    + " must implement BasicEditDialogListener");
//        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // In order to remove the top spacing bar in dialog before Android Lollipop
        // Actually it's a title bar in dialog. It's not happen on Android Marshmallow
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        View view = inflater.inflate(R.layout.dialog_fragment_basic_edit, container, false);
        ButterKnife.bind(this, view);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(editText, 0);
                           }

                       },
                300);


        // Multi line edit mode
        if (mIsMultiLine) {
            float dp = getResources().getDisplayMetrics().density;
            int width = (int) (300 * dp);
            int height = (int) (90 * dp);
            editText.setGravity(Gravity.TOP);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
            params.topMargin = (int) (28 * dp);
            params.bottomMargin = (int) (27 * dp);
            editText.setLayoutParams(params);
        }

        title.setText(mTitle);
        editText.setHint(mContentHint);
        editText.setInputType(mInputType);


        if (mTitle.equals("车牌号")) {
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
            editText.setTransformationMethod(new AllCapTransformationMethod());
            LogUtil.e(mTitle);
        }


        return view;
    }

    @OnClick(R.id.basic_edit_dialog_confirm_button)
    void confirmButtonTapped() {
        if (editText.getText().toString().isEmpty()) {
            Toast.makeText(getActivity(), "内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        if (mTitle.equals("手机号")) {
            if (!CheckForAllUtils.isMobileNO(editText.getText().toString())) {
                Toast.makeText(getActivity(), "手机号码不正确", Toast.LENGTH_SHORT).show();
                return;
            }
        }


        if (mListener != null) {
            mListener.onBasicEditDialogPositiveClick(this, editText.getText().toString());
        }


        getDialog().cancel();
    }

    @OnClick(R.id.basic_edit_dialog_cancel_button)
    void cancelButtonTapped() {
        getDialog().cancel();
    }
}
